package com.sq.subject.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.sq.subject.domain.bo.*;
import com.sq.subject.domain.handler.subject.SubjectTypeHandler;
import com.sq.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.sq.subject.domain.service.SubjectInfoDomainService;
import com.sq.subject.infra.basic.entity.SubjectInfo;
import com.sq.subject.infra.basic.entity.SubjectLabel;
import com.sq.subject.infra.basic.entity.SubjectMapping;
import com.sq.subject.infra.basic.service.SubjectInfoService;
import com.sq.subject.infra.basic.service.SubjectLabelService;
import com.sq.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author quan
 */
@Service
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectInfoService subjectInfoService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectsLabelService;

    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //新增题目详情
        SubjectInfo subjectInfo = BeanUtil.copyProperties(subjectInfoBO, SubjectInfo.class);
        subjectInfoService.insert(subjectInfo);
        subjectInfoBO.setSubjectId(subjectInfo.getId());
        //新增题目选项
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfoBO.getSubjectType());
        handler.add(subjectInfoBO);
        //新增题目分类及标签
        List<Long> categoryIds = subjectInfoBO.getCategoryIds();
        List<Long> labelIds = subjectInfoBO.getLabelIds();
        List<SubjectMapping> subjectMappingList = new ArrayList<>();
        categoryIds.stream().forEach(categoryId -> {
            labelIds.stream().forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setCategoryId(categoryId);
                subjectMapping.setLabelId(labelId);
                subjectMappingList.add(subjectMapping);
            });
        });
        subjectMappingService.batchInsert(subjectMappingList);
    }

    @Override
    public SubjectPageResultBO getSubjectPage(SubjectPageQueryBO subjectPageQueryBO) {
        SubjectInfo subjectInfo = BeanUtil.copyProperties(subjectPageQueryBO, SubjectInfo.class);
        List<SubjectInfo> subjectInfoList =  subjectInfoService.getSubjectPage(subjectInfo);
        SubjectPageResultBO subjectPageResultBO =  new SubjectPageResultBO();
        subjectPageResultBO.setPageNo(subjectPageQueryBO.getPageNo());
        subjectPageResultBO.setPageSize(subjectPageQueryBO.getPageSize());
        if(CollUtil.isEmpty(subjectInfoList)){
            subjectPageResultBO.setResult(Collections.emptyList());
            subjectPageResultBO.setTotal(0);
            return subjectPageResultBO;
        }
        List<SubjectPageBO> subjectPageBOList = BeanUtil.copyToList(subjectInfoList, SubjectPageBO.class);
        subjectPageBOList.forEach(subjectPageBO -> {
            subjectPageBO.setPageNo(subjectPageQueryBO.getPageNo());
            subjectPageBO.setPageSize(subjectPageQueryBO.getPageSize());
        });
        subjectPageResultBO.setResult(subjectPageBOList);
        subjectPageResultBO.setTotal(subjectInfoList.size());
        return subjectPageResultBO;
    }

    @Override
    public SubjectInfoBO querySubjectInfo(SubjectInfoBO subjectInfoBO) {
        Long subjectId = subjectInfoBO.getId();
        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectId);
        if(subjectInfo == null){
            throw new RuntimeException("题目不存在");
        }
        subjectInfoBO =  BeanUtil.copyProperties(subjectInfo, SubjectInfoBO.class);
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBO = handler.query(subjectInfoBO);
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setSubjectId(subjectInfoBO.getId());
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryByCondition(subjectMapping);
        if(CollUtil.isEmpty(subjectMappingList)){
            subjectInfoBO.setLabelNames(Collections.emptyList());
            return subjectInfoBO;
        }
        List<Long> labelIds = subjectMappingList.stream().map(SubjectMapping::getLabelId).toList();
        List<SubjectLabel> subjectLabelList = subjectsLabelService.batchQueryById(labelIds);
        List<String> labelNameList = subjectLabelList.stream().map(SubjectLabel::getLabelName).toList();
        subjectInfoBO.setLabelNames(labelNameList);
        return subjectInfoBO;
    }
}
